<?php

namespace App\Http\Controllers;
use App\IdentifyCode\IdentifyCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IdentifyController extends Controller{    // 短信验证
    public function sendIdentifyCode(Request $req){
        $url = "http://www.api.zthysms.com/sendSms.do";
        $username = 'EAI66hy';
        $password = 'eB2ORf';
        $tel = trim($req->tel);
        $type = intval(trim($req->type)) ? intval(trim($req->type)) : 0;
        $code = rand(100000,999999);
        if($type !=1){
            $content="【毅思科技】验证码$code,您正在注册成为 毅思科技 用户，请勿将验证码告知他人，谨防上当受骗，感谢您的支持！";
        }else{
            $content="【毅思科技】验证码$code,您正在修改您的登录密码，请勿将验证码告知他人，谨防上当受骗，感谢您的支持！";
        }
        $sendAPI = new IdentifyCode($url, $username, $password);
        
	$data = array(
		'content' 	=> $content,     //短信内容
		'mobile' 	=> $tel          //手机号码
	//	'xh'		=> ''//小号  
	);
      
	$sendAPI->data = $data;//初始化数据包
	$return = $sendAPI->sendSMS('POST');//GET or POST
       // dd($return);
        $res=array();
//        $res['code']=$code;
        if($return == 1){  //发送成功
            $req->session()->put('verifycode',array('code'=>$code));
            $res['status']=1;
        }else{               //发送失败
            $res['status']=0;
        }
        echo json_encode($res);
    }
    //检查验证码是否正确
    public function checkVerifyCode(Request $request){
        $code      = $request->code;
        $checkcode = $request->session()->get('verifycode')['code'];
        if($code == $checkcode){
            echo json_encode(array('status'=>1));
        }else{
            echo json_encode(array('status'=>0));
        }
    }
    
    public function sendIdentifyCodeAll(Request $req){  //批量发送
        $url = "http://www.api.zthysms.com/sendSmsBatch.do";
        $username = 'EAI66hy';
        $password = 'eB2ORf';
        $telInfo = DB::table('waihui_member')
                   ->pluck('tel')
                   ->toArray();
        
        $telList = implode(',', $telInfo);
        
        //$tel='13513312160,18537254319,15650740072';

        $content="【毅思科技】EAI认证分析师已经开始啦，各位老师‘喊’起来！神秘大奖等你来拿！另招募优秀全职分析师，详情请联系官方客服 400-8088-189";
        $sendAPI = new IdentifyCode($url, $username, $password);
        
        $data = array(
            'content' 	=> $content,     //短信内容
            'mobile' 	=> $telList         //手机号
        );
        
        $sendAPI->data = $data;//初始化数据包
        $return = $sendAPI->sendSMS('POST');
        dd($return);
        
        
        
        
	
    }
}
